package com.car.action.admin;

import java.text.ParseException;
import java.util.List;

import com.car.common.model.MessageUnreadNum;
import com.car.common.model.UserFans;
import com.car.common.model.UserSees;
import com.car.common.model.Users;
import com.car.common.model.VipRecord;
import com.car.intercept.AdminInterceptor;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.utils.DateUtil;
import com.utils.StringUtil;
import com.utils.SystemClock;

@Before(AdminInterceptor.class)
public class OrderController extends Controller  {
	
	public void index() {
		Db.update("update users set login_status = 1  where login_status = 0 and logout_time < " + SystemClock.now()/1000L);
		
		String sql = "select a.id,a.time,a.status,a.hot,a.alipay_account,a.order_num,a.title,a.create_time,a.update_time,a.buy_type,a.price,u.nick_name,u.user_mobile,a.user_id,u.vip_end_time,u.birthday,ae.name as area_name,u.sex,u.create_time as user_create_time,CASE u.sex  WHEN '1' THEN '男' WHEN '2' THEN '女' WHEN '3' THEN 'CD' WHEN '4' THEN 'FtM' WHEN '5' THEN 'MtF'  ELSE '其他' END as sex_name";
		
		sql += ", FROM_UNIXTIME(u.birthday,'%Y-%m-%d') as birthday_string, FROM_UNIXTIME(a.create_time,'%Y-%m-%d %H:%i') as create_time_string ,FROM_UNIXTIME(u.create_time,'%Y-%m-%d %H:%i') as u_create_time_string , FROM_UNIXTIME(u.vip_end_time,'%Y-%m-%d') as vip_end_time_string ";
		
		String andSql = " from vip_record as a left join users u on u.id = a.user_id left join area ae on ae.code = u.area_id where a.status = 1 and price > 0 ";
		
		if(!StringUtil.empty(getPara("user_id"))) {
        	andSql += " and a.user_id = " + getPara("user_id");
        }
		if(!StringUtil.empty(getPara("start_time"))) {
			try {
				andSql += " and a.create_time > " + DateUtil.commonDateTimeFormat.parse(getPara("start_time") + " 00:00:00").getTime() / 1000L;
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		
		if(!StringUtil.empty(getPara("end_time"))) {
			try {
				andSql += " and a.create_time < " + DateUtil.commonDateTimeFormat.parse(getPara("end_time") + " 23:59:59").getTime() / 1000L;
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		andSql += " order by a.id desc ";
        int page = 1;
        if(!StringUtil.empty(getPara("page"))) {
        	page = getParaToInt("page");
        } 
        Page<VipRecord> orderPage = VipRecord.dao.paginate(page, 15, sql,andSql);
        setAttr("page", orderPage);
        setAttr("user_id", getPara("user_id"));
        setAttr("start_time", getPara("start_time"));
        setAttr("end_time", getPara("end_time"));
        render("index.html");
	}
	
	
	
	public void index2() {
		String sql = "select a.id,a.time,a.status,a.hot,a.alipay_account,a.order_num,a.title,a.create_time,a.update_time,a.buy_type,a.price,u.nick_name,u.user_mobile,a.user_id,u.vip_end_time,u.birthday,ae.name as area_name,u.sex,u.create_time as user_create_time,CASE u.sex  WHEN '1' THEN '男' WHEN '2' THEN '女' WHEN '3' THEN 'CD' WHEN '4' THEN 'FtM' WHEN '5' THEN 'MtF'  ELSE '其他' END as sex_name";
		
		sql += ", FROM_UNIXTIME(u.birthday,'%Y-%m-%d') as birthday_string, FROM_UNIXTIME(a.create_time,'%Y-%m-%d %H:%i') as create_time_string ,FROM_UNIXTIME(u.create_time,'%Y-%m-%d %H:%i') as u_create_time_string , FROM_UNIXTIME(u.vip_end_time,'%Y-%m-%d') as vip_end_time_string ";
		
		String andSql = " from vip_record as a left join users u on u.id = a.user_id left join area ae on ae.code = u.area_id where a.status = 2 and price > 0 ";
		
		if(!StringUtil.empty(getPara("user_id"))) {
        	andSql += " and a.user_id = " + getPara("user_id");
        }
		if(!StringUtil.empty(getPara("start_time"))) {
			try {
				andSql += " and a.create_time > " + DateUtil.commonDateTimeFormat.parse(getPara("start_time") + " 00:00:00").getTime() / 1000L;
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		
		if(!StringUtil.empty(getPara("end_time"))) {
			try {
				andSql += " and a.create_time < " + DateUtil.commonDateTimeFormat.parse(getPara("end_time") + " 23:59:59").getTime() / 1000L;
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		andSql += " order by a.id desc ";
        int page = 1;
        if(!StringUtil.empty(getPara("page"))) {
        	page = getParaToInt("page");
        } 
        Page<VipRecord> orderPage = VipRecord.dao.paginate(page, 15, sql,andSql);
        setAttr("page", orderPage);
        setAttr("user_id", getPara("user_id"));
        setAttr("start_time", getPara("start_time"));
        setAttr("end_time", getPara("end_time"));
        render("index2.html");
	}
	
	public void embrace() {
		Db.update("update users set login_status = 1  where is_true > 1 and login_status = 0 and logout_time < " + SystemClock.now()/1000L);
		
		String sql = "select a.id,a.target_user_id,a.time,a.status,a.hot,a.alipay_account,a.order_num,a.title,a.create_time,a.update_time,a.buy_type,a.price,u.nick_name,u.user_mobile,a.user_id,u.vip_end_time,u.birthday,ae.name as area_name,u.sex,u.create_time as user_create_time,CASE u.sex  WHEN '1' THEN '男' WHEN '2' THEN '女' WHEN '3' THEN 'CD' WHEN '4' THEN 'FtM' WHEN '5' THEN 'MtF'  ELSE '其他' END as sex_name";
		
		sql += ", FROM_UNIXTIME(u.birthday,'%Y-%m-%d') as birthday_string, FROM_UNIXTIME(a.create_time,'%Y-%m-%d %H:%i') as create_time_string ,FROM_UNIXTIME(u.create_time,'%Y-%m-%d %H:%i') as u_create_time_string , FROM_UNIXTIME(u.vip_end_time,'%Y-%m-%d') as vip_end_time_string ";
		
		String andSql = " from vip_record as a left join users u on u.id = a.user_id left join area ae on ae.code = u.area_id where a.buy_type ='User' and a.status = 1 and (a.order_type = 4 or a.order_type = 5 ) ";
		// 
		
		if(!StringUtil.empty(getPara("user_id"))) {
        	andSql += " and a.user_id = " + getPara("user_id");
        }
		if(!StringUtil.empty(getPara("start_time"))) {
			try {
				andSql += " and a.create_time > " + DateUtil.commonDateTimeFormat.parse(getPara("start_time") + " 00:00:00").getTime() / 1000L;
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		
		if(!StringUtil.empty(getPara("end_time"))) {
			try {
				andSql += " and a.create_time < " + DateUtil.commonDateTimeFormat.parse(getPara("end_time") + " 23:59:59").getTime() / 1000L;
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		andSql += " order by a.id desc ";
        int page = 1;
        if(!StringUtil.empty(getPara("page"))) {
        	page = getParaToInt("page");
        } 
        Page<VipRecord> orderPage = VipRecord.dao.paginate(page, 15, sql,andSql);
        setAttr("page", orderPage);
        setAttr("user_id", getPara("user_id"));
        setAttr("start_time", getPara("start_time"));
        setAttr("end_time", getPara("end_time"));
        render("embrace.html");
	}
	
	
	public void follow_today() {
		String sql = "select id from users where is_true =  1 and sex = 2 and up_time >  " + ( SystemClock.now()/1000L -  24 * 90 * 60L)  ;
	 	List<Users> users = Users.dao.find(sql);
 	    for(Users u : users) {
 	    	int user_id = u.getId().intValue(); 
 	    	String sql1 = " SELECT t1.id FROM `users` AS t1 JOIN (SELECT ROUND(RAND() * (SELECT MAX(id) FROM `users`)) AS id) AS t2 WHERE t1.id >= t2.id and is_true > 1 and sex = 1 ORDER BY t1.id ASC LIMIT 1 ";
 	    	
 	    	Users users23 = Users.dao.findFirst(sql1);
 	    	if (users23 != null) {
 	    		int target_user_id = users23 .getId().intValue();
 	 			UserSees one1 = new UserSees();
 	 			one1.setUserId(target_user_id);
 	 			one1.setTargetUserId(user_id);
 	 			one1.setCreateTime(SystemClock.now()/1000L - 1 * 60);
 	 			one1.setUpdateTime(SystemClock.now()/1000L);
 	 			if (one1.save()) {
 	 				new CardController().giveABPoint(target_user_id);
 	 				MessageUnreadNum.dao.addNum(MessageUnreadNum.MESSAGE_UNREAD_TYPE_SEE, user_id);
 	 			}
 	 	    	//判断是否关注
 	 			UserFans one = UserFans.dao.findFirst("select id , user_id from user_fans where user_id = " + target_user_id+ " and target_user_id = " +  user_id +" and delete_time is null ");
 	 			if (one == null) {
 	 				one = new UserFans();
 	 				one.setUserId(target_user_id);
 	 				one.setTargetUserId(user_id);
 	 				one.setIsAuth(1);
 	 				one.setStatus(1);
 	 				one.setCreateTime(SystemClock.now()/1000L);
 	 				one.setUpdateTime(SystemClock.now()/1000L);
 	 				if (one.save()) {
 	 					new CardController().giveABPoint(target_user_id);
 	 					MessageUnreadNum.dao.addNum(MessageUnreadNum.MESSAGE_UNREAD_TYPE_FAN, target_user_id);
 	 				}	
 	 			}
 	    	}
 	    	 
 	    }
 	    renderText("操作成功!");
	}
	//select SUM(price) as price from  vip_record where buy_type != 'System'  and `status` = 1 and create_time > 1555171200
}
